package com.winderinfo.yikaotianxia.ui.shoppingcart;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.utils.LogUtil;
import com.winderinfo.yikaotianxia.core.utils.ToastUtil;
import com.winderinfo.yikaotianxia.ui.collection.CollectionBean;
import com.winderinfo.yikaotianxia.ui.collection.CollectionOptionAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShoppingCartAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ShoppingcartBean.YkCartListBean> list;
    //状态集合
    private Map<String, Boolean> checkbor = new HashMap<>();
    //价格集合
    private Map<String, Double> pricesss = new HashMap<>();

    public ShoppingCartAdapter(Context context, List<ShoppingcartBean.YkCartListBean> list) {
        this.context = context;
        this.list = list;
        getCheck(false);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_shopping, viewGroup, false);
        SelectedViewHolder selectedViewHolder = new SelectedViewHolder(view);
        return selectedViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, final int i) {
        final SelectedViewHolder selectedViewHolder = (SelectedViewHolder) viewHolder;
        selectedViewHolder.sho_title.setText(list.get(i).getYkGoods().getTitle());
        selectedViewHolder.sho_sum.setText("班型包括" + list.get(i).getYkGoods().getStar() + "门课程");
        selectedViewHolder.sho_price.setText("￥ " + list.get(i).getYkGoods().getPrice());
        //设置check的状态
        selectedViewHolder.sho_ches.setChecked(checkbor.get(list.get(i).getYkGoods().getId() + ""));
        //设置外面的check状态
        selectedViewHolder.sho_ches.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取价钱
                //清空价钱集合
                pricesss.clear();
                //获取当前id再次存入状态check的状态
                String id = list.get(i).getYkGoods().getId() + "";
                boolean checked = selectedViewHolder.sho_ches.isChecked();
                checkbor.put(id, checked);
                //初始化值
                boolean ischecl = true;
                //循环判断是否全部选中
                for (String key : checkbor.keySet()) {
                    Boolean aBoolean = checkbor.get(key);
                    //判断是否选中
                    if (!aBoolean) {//有没选中的返回false
                        ischecl = false;
                        if (onwisCheck != null) {
                            onwisCheck.getIsCheck(ischecl);
                            notifyDataSetChanged();
                        }
                        return;
                    }
                }
                if (ischecl) {//全部选中返回true
                    if (onwisCheck != null) {
                        onwisCheck.getIsCheck(ischecl);
                        notifyDataSetChanged();
                    }
                }
            }
        });
        //获取价格
        if (selectedViewHolder.sho_ches.isChecked()) {
            //选中框选中存入价格
            Double aDouble = Double.valueOf(list.get(i).getYkGoods().getPrice());
            pricesss.put(list.get(i).getYkGoods().getId() + "", aDouble);
            if (i == list.size() - 1) {//遍历完成调用
                getPrice();
            }
        } else {//没选择中也要调价钱的方法
            if (i == list.size() - 1) {//遍历完成调用
                getPrice();
            }
        }
        //商品详情
        selectedViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = list.get(i).getYkGoods().getId();
                String type = list.get(i).getYkGoods().getType();
                if (onId != null) {
                    onId.getOnId(id, type);
                }
            }
        });
        //删除
        selectedViewHolder.sho_butts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = list.get(i).getId();
                if (ondel!=null){
                    ondel.getDetId(id);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class SelectedViewHolder extends RecyclerView.ViewHolder {

        private final TextView sho_title, sho_sum, sho_price;
        private final CheckBox sho_ches;
        private final Button sho_butts;

        public SelectedViewHolder(@NonNull View itemView) {
            super(itemView);
            sho_title = itemView.findViewById(R.id.sho_title);
            sho_sum = itemView.findViewById(R.id.sho_sum);
            sho_price = itemView.findViewById(R.id.sho_price);
            sho_ches = itemView.findViewById(R.id.sho_ches);
            sho_butts = itemView.findViewById(R.id.sho_butts);
        }
    }

    //全选/全不选
    public void setIsCheck(boolean check) {
        //清空价钱集合
        pricesss.clear();
        getCheck(check);
        notifyDataSetChanged();
    }

    //设置按钮状态
    private void getCheck(boolean check) {
        for (int i = 0; i < list.size(); i++) {
            //获取商品id当键存入集合
            String id = list.get(i).getYkGoods().getId() + "";
            checkbor.put(id, check);
        }
    }

    //获取去价格
    public void getPrice() {
        //初始价格
        double money = 0;
        //循环获取价格
        for (String key : pricesss.keySet()) {
            money += pricesss.get(key);
        }
        //回传价格
        if (onPrice != null) {
            onPrice.getId(money);
        }
    }

    //接口回调jiage
    public interface OnPrice {
        void getId(double price);
    }

    public OnPrice onPrice;

    public void setOnPrice(OnPrice onPrice) {
        this.onPrice = onPrice;
    }

    //控制外面的选择框
    public interface OnwisCheck {
        void getIsCheck(boolean ischeck);
    }

    public OnwisCheck onwisCheck;

    public void setOnwisCheck(OnwisCheck onwisCheck) {
        this.onwisCheck = onwisCheck;
    }

    //商品详情接口
    public interface OnId {
        void getOnId(int id, String type);
    }

    public OnId onId;

    public void setOnId(OnId onId) {
        this.onId = onId;
    }

    //删除
    public interface Ondel {
        void getDetId(int id);
    }

    public Ondel ondel;

    public void setOndel(Ondel ondel) {
        this.ondel = ondel;
    }
}
